<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" :align-center="true">
		<el-form class="saveBox" :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="130px" label-position="top">
			<el-row :gutter="1">
				<el-col :span="5">
					<el-form-item label="工序计划单号" prop="plan_code">
						<el-input v-model="plan_code" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="工序名称" prop="process_name">
						<el-input v-model="process_name" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="物料编码" prop="material_code">
						<el-input v-model="material_code" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="物料名称" prop="material_name">
						<el-input v-model="material_name" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="5">
					<el-form-item label="工序状态" prop="produce_no">
						<el-input v-model="form.produce_no" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="委外" prop="is_out">
						<el-switch v-model="is_out" :active-value="1" :inactive-value="0" disabled></el-switch>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="物料规格" prop="material_spec">
						<el-input v-model="material_spec" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="单位" prop="unit_name">
						<el-input v-model="unit_name" placeholder=" " clearable readonly class="input"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="5">
					<el-form-item label="登录人员" prop="login_staff" v-if="is_out==0">
                        <el-select v-model="form.login_staff" filterable clearable placeholder="登录人员" style="width:100%">
                            <el-option v-for="item in clientList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
					</el-form-item>
                    <el-form-item label="供应商" prop="apply_id" v-else>
                        <el-select v-model="form.apply_id" filterable clearable placeholder="供应商" style="width:100%">
                            <el-option v-for="item in applyList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
					</el-form-item>
				</el-col>
                <el-col :span="5">
					<el-form-item label="可报工数" prop="have_work_number">
						<el-input v-model="have_work_number" type="number" readonly></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<!-- <el-form-item label="设备" prop="date">
						<el-input v-model="form.date" placeholder=" " clearable class="input"></el-input>
					</el-form-item> -->
				</el-col>
				<!-- <el-col :span="10">
					<el-form-item label="标准效率" prop="supplier_id">
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">/</span>
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">小时</span>
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">分钟</span>
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">秒</span>
					</el-form-item>
				</el-col> -->
			</el-row>
			<!-- <el-row :gutter="1">
				<el-col :span="5"></el-col>
				<el-col :span="5">
					<el-form-item label="良品数" prop="date">
						<el-input v-model="form.date" placeholder=" " clearable class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="10">
					<el-form-item label="实际效率" prop="supplier_id">
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">/</span>
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">小时</span>
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">分钟</span>
						<el-input style="width: 17%;" v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
						<span style="margin: 0 5px;">秒</span>
					</el-form-item>
				</el-col>
			</el-row> -->
			<!-- <el-row :gutter="1">
				<el-col :span="5">
					<el-form-item label="不良品项" prop="produce_no">
						<el-input v-model="form.produce_no" placeholder=" " clearable class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="不良品数" prop="supplier_id">
						<el-input v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="达标率(%)" prop="supplier_id">
						<el-input v-model="form.supplier_name" placeholder=" " clearable class="input"></el-input>
					</el-form-item>
				</el-col>
			</el-row> -->
			<el-row :gutter="1">
				<el-col :span="5">
					<el-form-item label="报工数" prop="work_number">
						<el-input v-model="form.work_number" type="number" class="input" @mousewheel.prevent @input="input"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="5">
					<el-form-item label="工序进度" prop="progress">
						<el-input v-model="form.progress" class="input" readonly>
                            <template #append>%</template>
                        </el-input>
					</el-form-item>
				</el-col>
				<el-col :span="10">
					<el-form-item label="描述" prop="remark">
						<el-input v-model="form.remark" clearable type="textarea" class="input"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="10">
					<el-form-item label="工作时长" prop="time">
						<el-input v-model="time" readonly class="input">
                            <template #append>时</template>
                        </el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	components: {

	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '报工',
				edit:'报工',
				show:'报工'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id:'',
                product_id: "",
                work_plan_id: "",
                work_id: "",
                login_staff: this.$TOOL.data.get("USER_INFO").admin_id,
                apply_id: "",
                work_number: "",
                progress: "",
                remark: "",
                check_status: "",
			},
			//验证规则
			rules: {
				end_price: [
					{ required: true, message: '请输入尾款' }
				],
			},
            plan_code: "",
            is_out:0,
            process_name: "",
            material_code: "",
            material_name: "",
            material_spec: "",
            unit_name: "",
            have_work_number: "",
            time: "",
            clientList: [],
            applyList: [],
		}
	},
	mounted() {
		this.getUser()
		this.getApply()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					res = await this.$API.production.report.add.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			// this.form.id = data.id
			this.form.work_plan_id = data.id
			this.form.product_id = data.product_id
			this.form.work_id = data.work_id
            this.is_out = data.work.is_out
            this.plan_code = data.work.work_code
            this.process_name = data.work.work_name
            this.material_code = data.product.goods_code
            this.material_name = data.product.goods_name
            this.material_spec = data.product.goods_spec
            this.unit_name = data.product.unit_name
            this.time = data.work_time
            // this.have_work_number = (data.machin.number * Number(data.route_detail.match)) - data.work_num
            this.have_work_number = data.can_num
		},
        // 获取人员
        async getUser(){
            var res = await this.$API.all.getAdmin.get({is_page:0});
            this.clientList = res.data;
        },
        // 获取供应商
        async getApply(){
            var res = await this.$API.all.getApply.get({is_page:0});
            this.applyList = res.data;
        },
        input(){
            if(this.form.work_number > this.have_work_number){
                this.$alert("报工数不能大于计划数量", "提示", {type: 'error'});
                this.form.work_number = this.have_work_number
                this.form.progress = (Number(this.form.work_number) / Number(this.have_work_number) * 100).toFixed(2)
            }else{
                this.form.progress = (Number(this.form.work_number) / Number(this.have_work_number) * 100).toFixed(2)
            }
        },
	}
}
</script>

<style lang="scss">
.el-form {
	max-height: 700px;
	overflow: hidden;
	overflow-y: auto;
}
.saveBox {
	.el-col {
		margin-right:
			30px;
	}
	.el-form-item {
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-start;
		.el-form-item__label {
			justify-content: flex-start;
		}
		.el-form-item__content {
			width: 100%;
		}
	}
}
</style>
